package classes;

import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

public class Player {
	
	private String name;
	private ArrayList<ArrayList<String>> coordinateList = new ArrayList<ArrayList<String>>();
	private ArrayList<ArrayList<String>> ships = new ArrayList<ArrayList<String>>();
	private ArrayBlockingQueue<String> lobby =  new ArrayBlockingQueue<String>(100);
	private ArrayBlockingQueue<String> shot =  new ArrayBlockingQueue<String>(100); //if other player shoots you
	private ArrayList<ArrayList<String>> iWasShot;  //for replay ([[row, col, shipId, wrecked(0/1)]]
	private ArrayBlockingQueue<String> status =  new ArrayBlockingQueue<String>(100);
	private ArrayList<Integer> cantShoot = new ArrayList<Integer>();
	private int wreckedShips;
	private boolean alreadyPlaying;
	private ArrayBlockingQueue<String> table =  new ArrayBlockingQueue<String>(100);
	
	
	public Player(String name){
		this.name = name;
		this.alreadyPlaying = false;
		this.wreckedShips = 0;
		this.iWasShot = new ArrayList<ArrayList<String>>();
		this.iWasShot.add(new ArrayList<String>());
	}
	
	public void init (){
		this.alreadyPlaying = false;
		this.wreckedShips = 0;
		this.iWasShot = new ArrayList<ArrayList<String>>();
		this.iWasShot.add(new ArrayList<String>());
	}
	
	public ArrayList<Integer> getCantShoot() {
		return cantShoot;
	}


	public ArrayBlockingQueue<String> getStatus() {
		return status;
	}


	public void setStatus(ArrayBlockingQueue<String> ready) {
		this.status = ready;
	}


	public ArrayBlockingQueue<String> getShot() {
		return shot;
	}

	public void setShot(ArrayBlockingQueue<String> shot) {
		this.shot = shot;
	}

	public boolean isAlreadyPlaying() {
		return alreadyPlaying;
	}

	public void setAlreadyPlaying(boolean alreadyPlaying) {
		this.alreadyPlaying = alreadyPlaying;
	}
	
	public ArrayBlockingQueue<String> getLobby() {
		return lobby;
	}

	public void setLobby(ArrayBlockingQueue<String> lobby) {
		this.lobby = lobby;
	}
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public  ArrayList<ArrayList<String>>  getCoordinateList() {
		return coordinateList;
	}

	public void setCoordinateList ( ArrayList<ArrayList<String>>  coordinates) {
		this.coordinateList = coordinates;
	}

	public ArrayList<ArrayList<String>> getShips() {
		return ships;
	}

	public void setShips(ArrayList<ArrayList<String>> ships) {
		this.ships = ships;
	}

	public int getWreckedShips() {
		return wreckedShips;
	}

	public void addWreckedShip() {
		this.wreckedShips += 1;
	}
	
	public boolean checkShips(){
		int shipCount = ships.size();
		int counter = 0;
		for (int i = 0; i < shipCount; i++){
			int length  = Character.getNumericValue(ships.get(i).get(0).charAt(4));
			int rowp = Integer.parseInt(ships.get(i).get(1));
			int colp = Integer.parseInt(ships.get(i).get(2));
			String positioning = ships.get(i).get(3);
			if(checkTouch(rowp, colp, length, positioning))
				return false;
			
		}
		for (int i=0; i<coordinateList.size(); i++){
			for (int j=0; j<coordinateList.get(i).size(); j++){
				if (!coordinateList.get(i).get(j).equals("0"))
					counter += 1;
			}
		}
		if (!(counter == 20))
			return false;
		else
			return true;
		
		
	}
	
	private boolean positionAllowed (int row, int col, int length, String positioning) {
	    if (positioning.equals("V")) {
	        if (row < 0 || (row + length-1) > 9 || col < 0 || col > 9) {
	            return true;
	        }
	        for (int i = row; i < row + length; i++) {
	            //alert("i = " + i + "; col = " + col);
	            if (!coordinateList.get(i).get(col).equals("0")) {
	                return false;
	            }
	        }
	        return true;
	    }
	    else {
	        if (row < 0 || row > 9 || col < 0 || (col + length-1) > 9) {
	            return true;
	        }
	        for (int i = col; i < col + length; i++) {
	            //alert("i = " + i + "; row = " + row);
	            //alert("pikkus on " + shipLocation[parseInt(row)][i].length);
	            if (!coordinateList.get(row).get(i).equals("0")) {
	                return false;
	            }
	        }
	        return true;
	    }
	}

	private boolean checkTouch (int row, int col, int length, String positioning) {
	    int rowp = row;
	    int colp = col;
	    int lenp = length;
	    if (positioning.equals("V")) {
	        if (!this.positionAllowed(rowp, colp - 1, lenp, positioning)) {
	            return true;
	        }
	        if (!this.positionAllowed(rowp, colp + 1, lenp, positioning)) {
	            return true;
	        }
	        for (int r = -1; r < 2; r++) {
	            if (!this.positionAllowed(rowp - 1, colp + r, 1, positioning)) {
	                return true;
	            }
	            if (!this.positionAllowed(rowp + lenp, colp + r, 1, positioning)) {
	                return true;
	            }
	        }

	    }
	    else if (positioning.equals("H")) {
	        if (rowp > 0 && !this.positionAllowed(rowp - 1, colp, lenp, positioning)) {
	            return true;
	        }
	        if (rowp < 9 && !this.positionAllowed(rowp + 1, colp, lenp, positioning)) {
	            return true;
	        }
	        for (int w = -1; w < 2; w++) {
	            if (colp > 0 && !this.positionAllowed(rowp + w, colp - 1, 1, positioning)) {
	                return true;
	            }
	            if (colp + lenp < 9 && !this.positionAllowed(rowp + w, colp + lenp, 1, positioning)) {
	                return true;
	            }
	        }

	    }
	    return false;
	}

	public ArrayBlockingQueue<String> getTable() {
		return table;
	}

	public void setTable(ArrayBlockingQueue<String> table) {
		this.table = table;
	}
	
	public void initCantShoot(){
		this.cantShoot = new ArrayList<Integer>();
	}

	public ArrayList<ArrayList<String>> getiWasShot() {
		return iWasShot;
	}

	public void setiWasShot(ArrayList<ArrayList<String>> iShoot) {
		this.iWasShot = iShoot;
	}
}
